#include <2025Linux.h>
#include <mysql/mysql.h> // 包含mysql的头文件
int main(int argc, char *argv[])
{
    MYSQL * conn = mysql_init(NULL); //给连接分配内存
    if(conn == NULL){
        fprintf(stderr,"mysql_init error: %s\n", mysql_error(conn));
        return -1;
    }
    MYSQL * ret =  mysql_real_connect(conn,
                       "localhost",
                       "root",
                       "123",//写自己的密码
                       "2025linux",//自己的database
                       0,NULL,0);
    if(ret == NULL){
        fprintf(stderr,"mysql_real_connect error: %s\n", mysql_error(conn));
        return -1;
    }

    char sql[4096] = "select * from pet;";

    int qret = mysql_query(ret,sql);//执行sql语句
    if(qret != 0){
        fprintf(stderr,"mysql_query error:%s\n", mysql_error(ret));
        return -1;
    }
    MYSQL_RES * result = mysql_store_result(ret);//取出保存的表格
    printf("row = %lu, col = %d\n", mysql_num_rows(result), mysql_num_fields(result));
    MYSQL_ROW row;//打算用来保存一行，字符串数组
    while((row = mysql_fetch_row(result)) != NULL){//一行一行地取出数据直到表格末尾
        for(unsigned int i = 0; i < mysql_num_fields(result); ++i){
            printf("%s\t",row[i]); //row是字符串数组 row[i]是字符串
        }
        printf("\n");
    }
    mysql_close(conn);
    return 0;
}

